﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Vue;

namespace AnimaGestionPersonnage.Component
{
    public partial class SecondarySkillComponent : UserControl
    {
        private int costSkill = 3;
        private SecondarySkill thisSkill;

        public SecondarySkillComponent()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Construit une composante avec la skill specifie.
        /// </summary>
        /// <param name="skill">La skill lié a cette composante</param>
        public SecondarySkillComponent(SecondarySkill s)
        {
            InitializeComponent();

            thisSkill = s;

            setSkill();
        }

        /// <summary>
        /// rempli les différents champs de la composante avec les données de
        /// cette skill
        /// </summary>
        /// <param name="skill">la skill source de données</param>
        public void setSkill()
        {
            int oldState = Utils.getCurrentState();
            
            Utils.setCurrentState(Utils.LOADING_STATE);

            costSkill = Utils.getSkillCost(thisSkill);
            labelSkillName.Text = thisSkill.name;
            numericUpDownTP.Value = thisSkill.tP;
            numericUpDownTP.Increment = costSkill;
            numericUpDownTP.Maximum = Utils.charac.totalTP;
            labelTPBonus.Text = System.Convert.ToString((float)(thisSkill.tP / costSkill));
            labelStatBonus.Text = System.Convert.ToString(Utils.getStatBonus(thisSkill.stat));
            numericUpDownNaturalBonus.Value = thisSkill.naturalBonus;
            numericUpDownNaturalBonus.Maximum = Utils.getLevel();
            labelNatBonus.Text = System.Convert.ToString(Utils.getNatBonus(thisSkill));
            labelTotalBonus.Text = System.Convert.ToString(Utils.getTotal(thisSkill));
            labelLvlBonus.Text = System.Convert.ToString(Utils.getLevelBonus(thisSkill));
            numericUpDownOtherBonus.Value = thisSkill.otherBonus;

            Utils.setCurrentState(oldState);
        }

        public void updateSkill()
        {
            foreach (SecondarySkill s in Utils.charac.secondarySkills)
            {
                if (s.Equals(thisSkill))
                {
                    s.tP = thisSkill.tP;
                    break;
                }
            }
        }

        public TypeSkill getTypeSkill()
        {
            return thisSkill.type;
        }

        #region EVENT BOXES

        private void numericUpDownTP_ValueChanged(object sender, EventArgs e)
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                if (Utils.charac.TPAvail >= ((int)numericUpDownTP.Value - thisSkill.tP))
                {
                    Utils.charac.TPAvail -= ((int)numericUpDownTP.Value - thisSkill.tP);
                    thisSkill.tP = (int)numericUpDownTP.Value;
                    labelTPBonus.Text = System.Convert.ToString((float)(thisSkill.tP / costSkill));
                    labelTotalBonus.Text = System.Convert.ToString(Utils.getTotal(thisSkill));
                    ((FormPFSpending)ParentForm).setLabelTPLeft();
                    updateSkill();
                }
                else
                {
                    MessageBox.Show("Trop de PF assignés.", "Erreur",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Error);
                    numericUpDownTP.Value = thisSkill.tP;
                }
            }

        }

        private void numericUpDownNaturalBonus_ValueChanged(object sender, EventArgs e)
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                if (Utils.charac.naturalBonusAvail >= ((int)numericUpDownNaturalBonus.Value - thisSkill.naturalBonus))
                {
                    Utils.charac.naturalBonusAvail -= ((int)numericUpDownNaturalBonus.Value - thisSkill.naturalBonus);
                    thisSkill.naturalBonus = (int)numericUpDownNaturalBonus.Value;
                    labelNatBonus.Text = System.Convert.ToString(Utils.getNatBonus(thisSkill));
                    labelTotalBonus.Text = System.Convert.ToString(Utils.getTotal(thisSkill));
                    ((FormPFSpending)ParentForm).setLabelNaturalBonusLeft();
                    foreach (SecondarySkill s in Utils.charac.secondarySkills)
                    {
                        if (s.Equals(thisSkill))
                        {
                            s.naturalBonus = thisSkill.naturalBonus;
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Trop de bonus naturel assignés.", "Erreur",
                                                                MessageBoxButtons.OK,
                                                                MessageBoxIcon.Error);
                    numericUpDownNaturalBonus.Value = thisSkill.naturalBonus;
                }
            }
        }

        private void numericUpDownOtherBonus_ValueChanged(object sender, EventArgs e)
        {
            if (Utils.getCurrentState() != Utils.LOADING_STATE)
            {
                thisSkill.otherBonus = (int)numericUpDownOtherBonus.Value;
                labelTotalBonus.Text = System.Convert.ToString(Utils.getTotal(thisSkill));
                foreach (SecondarySkill s in Utils.charac.secondarySkills)
                {
                    if (s.Equals(thisSkill))
                    {
                        s.otherBonus = thisSkill.otherBonus;
                        break;
                    }
                }
            }
        }

        #endregion
    }
}
